import re

import act.seeker as se
import act.tools as tools
from act.message_struct import MessageStruct


#获取某个对话最新的消息
def get_last_message(chat_shot):
    chat_shot_inner = chat_shot.GetChildren()[0]
    children = chat_shot_inner.GetChildren()
    if len(children) >= 3:
        origin_message = children[1].GetChildren()[1].GetChildren()[0].Name
        msg = origin_message.split('：', 1)
        if len(msg) >= 2:
            return msg[1]
        else:
            return msg[0]
    return None


def get_last_messages(result, window_control, messageControl, new_message_num):
    count = 0
    children = messageControl.GetChildren()
    index = len(children) - 1
    while count < new_message_num:
        content = children[index].Name
        # 过滤掉时间
        details = children[index].GetChildren()[0].GetChildren()
        if len(details) == 0:
            index = index - 1
            continue
        elif content in ['以下为新消息',
                         '发出红包，请在手机上查看',
                         '收到红包，请在手机上查看',
                         '你发送了一次转账收款提醒，请在手机上查看',
                         '你收到了一次转账收款提醒，请在手机上查看']:
            index = index - 1
            continue
        elif (('加入了群聊' in content)
              or ('移出了群聊' in content)
              or ('撤回了一条消息' in content)
              or ('与群里其他人都不是朋友关系，请注意隐私安全' in content)
              or ('领取了你的红包') in content):
            index = index - 1
            continue
        elif content == '查看更多消息':
            window_control.WheelUp(wheelTimes=20)
            messageControl = window_control.ListControl(Name='消息')
            new_msg_button = se.get_one_session_new_msg_button(window_control)
            if new_msg_button is not None:
                notification = new_msg_button.Name
                match = re.match(r"(\d+)条新消息", notification)
                num = int(match.group(1))
                result.clear()
                get_last_messages(result, window_control, messageControl, new_message_num + num)
                return
            new_messages = messageControl.GetChildren()
            index = len(new_messages) - len(children)
            children = new_messages
            continue
        result.append(content)
        index = index - 1
        count = count + 1


# 获取当天至最新的聊天记录
def get_last_today_message(session, window_control):
    # 点击会话获得相应聊天
    session.Click()
    messageControl = window_control.ListControl(Name='消息')
    result = []
    for message in messageControl.GetChildren():
        break


# 获取当天至最新的聊天记录
def get_last_today_message_by_RPA(session, window_control):
    # 点击会话获得相应聊天
    session.Click()
    window_control.WheelUp(wheelTimes=10)
    messageControl = window_control.ListControl(Name='消息')
    # 初始化一个空字典
    result_map = {}
    key = 0
    key_wait_to_remove = []
    messages = messageControl.GetChildren()
    index = len(messages) - 1
    while index >= 0:
        # 先判断是不是今天的
        # 再点击查看更多消息
        content = messages[index].Name
        details = messages[index].GetChildren()[0].GetChildren()
        if len(details) == 0:
            msg = MessageStruct('Time', 'System', content)
            if ('昨天' in content) or ('日' in content):
                result_map[key] = msg
                key_wait_to_remove.append(key)
                break
            else:
                result_map[key] = msg
                key_wait_to_remove.append(key)
                key = key + 1
                index = index - 1
        elif (('加入了群聊' in content)
              or ('移出了群聊' in content)
              or ('撤回了一条消息' in content)
              or ('与群里其他人都不是朋友关系，请注意隐私安全' in content)
              or ('领取了你的红包' in content)
              or ('修改群名为' in content)):
            index = index - 1
            continue
        elif content in ['以下为新消息',
                         '发出红包，请在手机上查看',
                         '收到红包，请在手机上查看',
                         '你发送了一次转账收款提醒，请在手机上查看',
                         '你收到了一次转账收款提醒，请在手机上查看']:
            index = index - 1
            continue
        elif content == '查看更多消息':
            window_control.WheelUp(wheelTimes=20)
            messageControl = window_control.ListControl(Name='消息')
            new_messages = messageControl.GetChildren()
            index = len(new_messages) - len(messages)
            messages = new_messages
        else:
            name = messages[index].ButtonControl(foundIndex=1).Name
            msg = MessageStruct('Content', name, content)
            result_map[key] = msg
            key = key + 1
            index = index - 1
    # 去掉这个时间点之前的聊天，因为不在当天
    if len(key_wait_to_remove) >= 1:
        if (len(key_wait_to_remove) == 1):
            for del_key in range(0, key_wait_to_remove[0]):
                del result_map[del_key]
        else:
            key = key_wait_to_remove[-2]
            for del_key in range(key + 1, key_wait_to_remove[-1]):
                del result_map[del_key]
            # 去掉微信产生的聊天时间:
        for time_key in key_wait_to_remove:
            del result_map[time_key]
    result = []
    for value in reversed(result_map.values()):
        result.append(value)
    return result


if __name__ == '__main__':
    weChat_window = se.get_window()
    result = []
    messageControl = weChat_window.ListControl(Name='消息')
    get_last_messages(result, weChat_window, messageControl, 50)
    print(len(result))
    print(result)